//
//  LDShowPicVC.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/6/10.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit

class LDShowPicVC: UIViewController {

    var imgUrl = String()
    
    
    var imageView:UIImageView!
    var saveBtn:UIButton!
    var indActivity:UIActivityIndicatorView!
    
    var requestImg:UIImage?
    var dissclosure:(()->Void)?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setDefaultContent()
        
    }
    
    override func viewWillAppear(animated: Bool) {
        
        
        indActivity.startAnimating()
        
        UIView.animateWithDuration(0.6) { [weak self] in
            UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.Default, animated: true)
            self?.imageView.center = (self?.view.center)!
            
            print(self?.imageView.center)
            self?.imageView.frame = CGRectMake(0, (LDFrame.ScreenHeight - LDFrame.ScreenWidth)/2, LDFrame.ScreenWidth, LDFrame.ScreenWidth)
        }
        imageView.sd_setImageWithURL(NSURL(string: imgUrl), placeholderImage: UIImage(named: "timeline_image_loading")) {
            [weak self](image, error, type, url) in
            
            self?.requestImg = image
            self?.indActivity.stopAnimating()
        }
    }
    
    
    func setDefaultContent() {
        
        
        view.frame = LDFrame.ScreenBounds
        
        indActivity = UIActivityIndicatorView.init(activityIndicatorStyle: .WhiteLarge)
        indActivity.center = view.center
        
        imageView = UIImageView()
        imageView.contentMode = .ScaleAspectFit
        imageView.center = view.center
        
        saveBtn = UIButton.setDetailsBtn("保存", picName: nil, btnBackColor: nil, titleColor: LDFontColor.White)
        saveBtn.bk_whenTapped { [weak self] in
            
            if self?.requestImg == nil {
                
                LDShowHUB.shareInstance.showBarStatus("保存错误")
                return
            }
            LDShowHUB.shareInstance.showToastStatus("保存中...")
            UIImageWriteToSavedPhotosAlbum((self?.requestImg)!, self,  #selector(self!.didSavaImgToAlblum), nil)
            
        }
        saveBtn.frame = CGRectMake(30, LDFrame.ScreenHeight - 50, 35, 25)
        
        view.addSubview(imageView)
        view.addSubview(saveBtn)
        view.addSubview(indActivity)
        
        view.userInteractionEnabled = true
        imageView.userInteractionEnabled = true
        imageView.multipleTouchEnabled = true
        //添加手势
        
        let geasure = UIPinchGestureRecognizer.init(target: self, action: #selector(scalePic(_:)))
        imageView.addGestureRecognizer(geasure)
        
        view.bk_whenTapped { [weak self] in
            
            UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: false)
            self?.dismissViewControllerAnimated(false, completion: nil)
            
            if self?.dissclosure != nil {
                self?.dissclosure!()
            }
        }
        
        imageView.bk_whenTapped { [weak self] in
            
            UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: false)
            self?.dismissViewControllerAnimated(false, completion: nil)
            
            if self?.dissclosure != nil {
                self?.dissclosure!()
            }
        }
    }
    
    func didSavaImgToAlblum(image:UIImage?,error:NSError?,contextInfo:AnyObject) {
        
        if error == nil {
//            LDShowHUB.shareInstance.showBarStatus("保存成功")
            LDShowHUB.shareInstance.showToastSucc("保存成功")
        } else {
            LDShowHUB.shareInstance.showToastSucc("保存失败")
        }
        
    }
    func vcDissmiss(closure:()->Void) {
        
        dissclosure = closure
    }
    
    func scalePic(gesture:UIPinchGestureRecognizer) {
        
        var scale = gesture.scale;

    
        if (gesture.state == UIGestureRecognizerState.Began) {
            
//            imageView.frame = CGRectMake(0, (LDFrame.ScreenHeight - LDFrame.ScreenWidth)/2, LDFrame.ScreenWidth, LDFrame.ScreenWidth)
//            picScale = imgpic.size.width / imgpic.size.height
            
        }
        
        //离开scale为1.0
        if gesture.state == UIGestureRecognizerState.Ended {
            
            
//            imageView.frame = CGRectMake(0, (LDFrame.ScreenHeight - LDFrame.ScreenWidth)/2, LDFrame.ScreenWidth, LDFrame.ScreenWidth)

            UIView.animateWithDuration(0.2, animations: { [weak self] in
            
                self?.imageView.frame = CGRectMake(0, (LDFrame.ScreenHeight - LDFrame.ScreenWidth)/2, LDFrame.ScreenWidth, LDFrame.ScreenWidth)
//                self?.imageView.center = (self?.view.center)!
//                self?.imageView.frame.size = CGSizeMake(LDFrame.ScreenWidth, LDFrame.ScreenWidth)
            })
        }
        
        // 对图片进行缩放
        if scale > 1.5 || scale < 0.5 {
            
            scale = scale > 1.5 ? 1.5:0.5
            
        }
        
        if gesture.state == UIGestureRecognizerState.Changed {
            
            UIView.animateWithDuration(0.1) { [weak self] in
                
                print("11",self?.imageView.center)
                let width = LDFrame.ScreenWidth * scale
                let height = LDFrame.ScreenHeight * scale
                
                self?.imageView.frame.size = CGSizeMake(width, height)
                self?.imageView.center = (self?.view.center)!
                
            }
            
        }
    }

}
